-
Notifications
You must be signed in to change notification settings - Fork 220
Support configuring Html props from react-server #1661
Conversation
react-html's <Html> component supports a bunch of props for configuring content to inject into the head, however previously that was not exposed from react-server's createRender and createServer functions. This commit lets you pass a htmlProps option into createRender and createServer that will then be applied to the Html element. This means you can use createServer, and inject arbitary content into the head using <Html>'s headMarkup prop (and use any of <Html>'s other props too)
c3d21b4
to
c0736df
Compare
manager?: HtmlManager; | ||
hydrationManager?: HydrationManager; | ||
children: React.ReactElement<any> | string; | ||
children?: React.ReactElement<any> | string; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exporting HtmlProps and marking children as optional means I can reuse this type down in the definition of createRender's options object (https://github.com/Shopify/quilt/pull/1661/files#diff-3db342f047695e4ca4277cd8264ac3b2db110ec15ec6c34ed697ce2859004df0R57) rather than creating a new type that omits children
@@ -133,8 +148,16 @@ export function createRender(render: RenderFunction, options: Options = {}) { | |||
assets.scripts({name: assetName, asyncAssets: immediateAsyncAssets}), | |||
]); | |||
|
|||
styles.push(...additionalStyles); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Making sure that if somebody decides to use styles/scripts then they don't clobber the assets provided by getAssets
. I've added a test case for this.
@@ -18,10 +17,11 @@ interface Options { | |||
port?: number; | |||
assetPrefix?: string; | |||
proxy?: boolean; | |||
assetName?: string | ValueFromContext<string>; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Strictly speaking the change to assetName and renderError isn't needed as this is functionally identical, but I figure reusing the existing type is nicer than having to repeat the declarations.
Would it make sense to add an example in the README, showing what's now possible to do? |
@kaelig I added to the README section for what createServer accepts: |
styles: additionalStyles = [], | ||
...additionalHtmlProps | ||
} = | ||
typeof htmlPropsInput === 'function' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit nitpicky but I found this a bit difficult to parse at first glance because of the line break.
Maybe split this up into 2 expressions by pulling out this ternary into a const props
or something.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree, the linebreak after the =
is quite unconventional. Not sure it's worth adding a level of indirection to fix this, though.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
might be hard as there probably aren't many demo apps lying around.
A majority of our createServer
use cases are abtracted behind the react-server webpack plugin. I don't foresee any impact on those cases with this PR (I didn't tophat). 🎩ing this is challenging without a beta release of some sort. If you feel confident with these changes, that's 👍 with me. If you want to run beta release, I can give this a 🎩 .
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the README update, I approve the README side of things! 👍
Co-authored-by: Kaelig Deloumeau-Prigent <[email protected]>
Description
react-html's
<Html>
component supports a bunch of props for configuringcontent to inject into the head, however previously that was not exposed
from react-server's createRender and createServer functions.
This commit lets you pass a htmlProps option into createRender and
createServer that will then be applied when createRender calls
<Html>
.This means you can use createServer, and inject arbitary content into
the head using
<Html>
's headMarkup prop (and use any of<Html>
's otherprops too):
To test
An hand-cranked end-to-end test of this might be hard as there probably aren't many demo apps lying around.
Find an application that uses
createServer
within it's server entrypoint (brand-registry does this but I'm otherwise unfamiliar with the app). Add 'htmlProps' to its list of options, and then note that the markup is injected into the head:Fortunately the createServer tests act as automated end-to-end test by configuring the server and asserting what html gets returned. this new unit test demonstrates adding
htmlProps
to the call tocreateServer
and asserting that the returned html includes that head markup.So the real tophat instructions are "check tests pass, check the added unit tests showcase e2e behaviour" :D
Type of change
Checklist